/*
 * File: FrameRateCounter.cs
 * Author: Gourav Das
 * Purpose: Displays Framerate
 * Created: April 30th 2011
 * Copyright: Gourav Das, 2011
 */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

namespace _22yards.Helpers
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class FrameRateCounter : DrawableGameComponent
    {
        ContentManager content;
        SpriteBatch spriteBatch;
        SpriteFont spriteFont;

        int frameCount = 0;
        TimeSpan elapsedTime = TimeSpan.Zero;

        /// <summary>
        /// Gets the frame rate.
        /// </summary>
        public int FrameRate { get ; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="FrameRateCounter"/> class.
        /// </summary>
        /// <param name="game">The Game that the game component should be attached to.</param>
        public FrameRateCounter(Game game)
            : base(game)
        {
            content = new ContentManager(game.Services,"22yardsContent");
        }


        /// <summary>
        /// Called when graphics resources need to be loaded. Override this method to load any component-specific graphics resources.
        /// </summary>
        protected override void LoadContent()
        {
                spriteBatch = new SpriteBatch(GraphicsDevice);
                spriteFont = content.Load<SpriteFont>("Helpers/Fonts/FPSFont");
        }


        /// <summary>
        /// Called when graphics resources need to be unloaded. Override this method to unload any component-specific graphics resources.
        /// </summary>
        protected override void UnloadContent()
        {
                content.Unload();
        }


        /// <summary>
        /// Called when the GameComponent needs to be updated. Override this method with component-specific update code.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to Update</param>
        public override void Update(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime > TimeSpan.FromSeconds(1))
            {
                elapsedTime -= TimeSpan.FromSeconds(1);
                FrameRate = frameCount;
                frameCount = 0;
            }
        }


        /// <summary>
        /// Called when the DrawableGameComponent needs to be drawn. Override this method with component-specific drawing code. Reference page contains links to related conceptual articles.
        /// </summary>
        /// <param name="gameTime">Time passed since the last call to Draw.</param>
        public override void Draw(GameTime gameTime)
        {
            frameCount++;

            spriteBatch.Begin();

            //Displays with shadow
            spriteBatch.DrawString(spriteFont, "fps: " + FrameRate, new Vector2(33, 33), Color.Black);
            spriteBatch.DrawString(spriteFont, "fps: " + FrameRate, new Vector2(32, 32), Color.White);

            spriteBatch.End();
        }
    }
}
